﻿using System;
using System.ComponentModel;
using System.ServiceModel.Channels;
using System.ServiceModel;

namespace PolePosition.v3.ServiceProxy
{
    public class Setup
	{
		public enum BindingTypes
        {
            HTTP,
            HTTPSecure
        }

        private static string m_BaseUrl;
        public static string BaseUrl 
        {
            get { return m_BaseUrl;  }
            set 
            {
                if (!string.IsNullOrEmpty(value) && !value.EndsWith("/"))
                    value += "/";

                m_BaseUrl = value;  
            }
        }

        private static BindingTypes m_DefaultBinding = BindingTypes.HTTP;
        public static BindingTypes DefaultBinding 
        {
            get { return m_DefaultBinding;  }
            set { m_DefaultBinding = value;  }
        }

        private static Binding m_Binding;
        public static Binding Binding 
        {
            get 
            {
                switch (m_DefaultBinding)
                {
                    case BindingTypes.HTTP:
                        if (m_Binding == null || m_Binding.GetType() != typeof(BasicHttpBinding))
                            m_Binding = new BasicHttpBinding("BasicHttpBinding_Proxy");
                        break;
                    case BindingTypes.HTTPSecure:
                        if (m_Binding == null || m_Binding.GetType() != typeof(WSHttpBinding))
                            m_Binding = new WSHttpBinding("WsHttpBinding_Proxy");
                        break;
                    default:
                        break;
                }
                return m_Binding;  
            }
        }

        internal static EndpointAddress GetEndPointAddress(string serviceFileName)
        {
            EndpointAddress _EndPointAddress;
            string _appendEndpointType = string.Empty;
            
            switch (DefaultBinding)
            {
                case BindingTypes.HTTPSecure:
                    _appendEndpointType = "/http_secure";
                    break;
                case BindingTypes.HTTP:
                default:
                    break;
            }

            _EndPointAddress = new EndpointAddress(string.Format("{0}{1}{2}", Setup.BaseUrl, serviceFileName, _appendEndpointType));

            return _EndPointAddress;
        }
	}
}
